package com.letv.so.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 拼音工具
 * 
 * @author wenjian
 * 
 */
public class PinyinUtil {
	protected final static Logger logger = LoggerFactory.getLogger(PinyinUtil.class);

	private static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

	static {
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 没有音标，默认数字音标
		// format.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 大写，默认小写
		format.setVCharType(HanyuPinyinVCharType.WITH_V);// // 将'ü'格式化为'v'，默认格式化为'u:'
	}

	/**
	 * 返回拼音组合，非汉字则直接返回
	 * 
	 * @param c
	 * @return
	 */
	private static List<String> getPinyinList(char c) {
		List<String> pinyinList = new ArrayList<String>();
		try {
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
			if (pinyinArray == null) {// 特殊字符直接返回
				pinyinList.add(String.valueOf(c));
			} else {// 拼音去重复
				Set<String> pinyinSet = new HashSet<String>();
				for (String pinyin : pinyinArray) {
					if (pinyinSet.add(pinyin)) {
						pinyinList.add(pinyin);
					}
				}
			}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			logger.error(e.getMessage());
		}

		return pinyinList;
	}

	/**
	 * 获得一个词的拼音组合个数
	 * 
	 * @param str
	 * @return
	 */
	@SuppressWarnings("unused")
	private static int getPinyiListSize(String str) {
		if (str == null || "".equals(str)) {
			return 0;
		}
		int i = 1;
		try {
			char[] cArray = str.toCharArray();
			for (char c : cArray) {
				List<String> pinyinList = getPinyinList(c);
				if (pinyinList != null) {
					i = i * pinyinList.size();
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return i;
	}

	/**
	 * 获得一个词的拼音组合递归方法
	 * 
	 * @param str
	 * @param pinyinList_
	 * @return
	 */
	private static List<String> getPinyinList(String str, List<String> pinyinList_) {
		if ("".equals(str)) {
			return pinyinList_;
		} else {
			List<String> pinyinList = new ArrayList<String>();
			char c = str.charAt(0);
			List<String> _pinyinList = getPinyinList(c);
			for (String pinyin_ : pinyinList_) {
				for (String _pinyin : _pinyinList) {
					pinyinList.add(pinyin_ + _pinyin);
				}
			}
			return getPinyinList(str.substring(1), pinyinList);
		}
	}

	/**
	 * 获得一个词的简拼组合递归方法
	 * 
	 * @param str
	 * @param jianpinList_
	 * @return
	 */
	private static List<String> getJianpinList(String str, List<String> jianpinList_) {
		if ("".equals(str)) {
			return jianpinList_;
		} else {
			List<String> pinyinList = new ArrayList<String>();
			char c = str.charAt(0);
			List<String> _jianpinList = getPinyinList(c);
			for (String pinyin_ : jianpinList_) {
				for (String _pinyin : _jianpinList) {
					pinyinList.add(pinyin_ + _pinyin.charAt(0));
				}
			}
			return getJianpinList(str.substring(1), pinyinList);
		}
	}

	/**
	 * 获得一个词的拼音组合数组
	 * 
	 * @param str
	 * @return
	 */
	public static List<String> getPinyinList(String str) {
		if (str == null || "".equals(str)) {
			return null;
		}
		List<String> pinyinList = new ArrayList<String>();
		pinyinList.add("");
		return getPinyinList(str, pinyinList);
	}

	/**
	 * 获得一个词的拼音组合字符串，多拼音用空格分隔
	 * 
	 * @param str
	 * @param limit
	 * @return
	 */
	public static String getPinyinString(String str, int limit) {
		StringBuilder pinyins = new StringBuilder();
		List<String> pinyinList = getPinyinList(str);
		if (pinyinList != null) {
			int i = 1;
			for (String pinyin : pinyinList) {
				if (i > limit) {// 达到限制个数
					break;
				}
				pinyins.append(pinyin);
				if (i < pinyinList.size() && i < limit) {
					pinyins.append(" ");
				}
				i++;
			}
		}
		return pinyins.toString();
	}

	/**
	 * 获得一个词的简拼组合数组
	 * 
	 * @param str
	 * @return
	 */
	public static List<String> getJianpinList(String str) {
		if (str == null || "".equals(str)) {
			return null;
		}
		List<String> pinyinList = new ArrayList<String>();
		pinyinList.add("");
		return getJianpinList(str, pinyinList);
	}

	/**
	 * 获得一个词简拼组合字符串，多简拼用空格分隔
	 * 
	 * @param str
	 * @param limit
	 * @return
	 */
	public static String getJianpinString(String str, int limit) {
		StringBuilder jianpins = new StringBuilder();
		List<String> jianpinList = getJianpinList(str);
		if (jianpinList != null) {
			int i = 1;
			for (String jianpin : jianpinList) {
				if (i > limit) {// 达到限制个数
					break;
				}
				jianpins.append(jianpin);
				if (i < jianpinList.size() && i < limit) {
					jianpins.append(" ");
				}
				i++;
			}
		}
		return jianpins.toString();
	}

	/**
	 * 获得一个词的首字母组合数组
	 * 
	 * @param str
	 * @return
	 */
	public static List<String> getPinyinPrefixList(String str) {
		if (str == null || "".equals(str)) {
			return null;
		}
		return getJianpinList(String.valueOf(str.charAt(0)));
	}
	  /** 
     * 处理特殊字符
     * 
     * @param chinese 汉字串 
     * @return 汉语拼音 
     */ 
    public static String formatStr(String chinese) { 
		String chn = chinese.replace("0", "零").replace("1", "一")
				.replace("2", "二").replace("3", "三").replace("4", "四")
				.replace("5", "五").replace("6", "六").replace("7", "七")
				.replace("8", "八").replace("9", "九").replace("Ⅰ", "一")
				.replace("Ⅱ", "二").replace("Ⅲ", "三").replace("Ⅳ", "四")
				.replace("Ⅴ", "五").replace("ⅰ", "一").replace("ⅱ", "二")
				.replace("ⅲ", "三").replace("ⅳ", "四").replace("ⅴ", "五");
		return chn;
    } 

	public static void method2(String num) {
		String[] arrUnit = { "元", "十", "百", "千", "万" }; // 计量单位
		String[] arrNum = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; // 大写数字
		String[] strNum = num.split("|");
		int nNumCount = strNum.length - 2;
		for (int i = 0; i < strNum.length; i++) {
			if (strNum[i] != null && !"".equals(strNum[i])) {
				int n = Integer.parseInt(strNum[i]);
				switch (n) {
				case 0:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 1:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 2:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 3:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 4:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 5:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 6:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 7:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 8:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				case 9:
					System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
					break;
				}
			}
		}
	}
	/**
	 * 测试
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		method2("10天");
	}
}
